package com.bt.design.common.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串处理类 重要工具类
 * 
 * @author lshua
 *
 */
public class StringUtil {
	
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
    
    public static String upperCase(final String str) {
        if (str == null) {
            return null;
        }
        return str.toUpperCase();
    }

	/**
	 * 判断对象是否为空
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj) {
		return obj == null || obj.toString().equals("") || obj.equals("null")
				|| obj.equals("NULL") ? true : false;
	}

	/** 基本数据类型的转换 ***/

	/**
	 * 将空类型转为字符串型
	 * 
	 * @param obj
	 * @return
	 */
	public static String null2String(Object obj) {
		return obj == null || obj.equals("null") || obj.equals("NULL") ? ""
				: obj.toString();
	}

	/**
	 * 转为Long类型，空值返回null
	 * 
	 * @param obj
	 * @return
	 */
	public static Long strToLong(Object obj) {
		return isEmpty(obj) ? null : Long.parseLong(obj.toString());
	}

	/**
	 * 转为Int类型
	 * 
	 * @param str
	 * @return
	 */
	public static Integer strToInt(Object obj) {
		return isEmpty(obj) ? null : Integer.parseInt(obj.toString());
	}

	/**
	 * 转为Float类型
	 * 
	 * @param str
	 * @return
	 */
	public static Float strToFloat(Object obj) {
		return isEmpty(obj) ? null : Float.parseFloat(obj.toString());
	}

	/**
	 * 转为Double类型
	 * 
	 * @param str
	 * @return
	 */
	public static Double strToDouble(Object obj) {
		return isEmpty(obj) ? null : Double.parseDouble(obj.toString());
	}

	public static Boolean strToBoolean(Object obj) {
		return isEmpty(obj) ? false : Boolean.parseBoolean(obj.toString());
	}

	public static Character strToChar(Object obj) {
		return isEmpty(obj) ? ' ' : (Character) obj;
	}

	public static Byte strToByte(Object obj) {
		return isEmpty(obj) ? 0 : Byte.parseByte(obj.toString());
	}

	public static Short strToShort(Object obj) {
		return isEmpty(obj) ? 0 : Short.parseShort(obj.toString());
	}

	/**
	 * 转为long类型
	 * 
	 * @param str
	 * @return
	 */
	public static long str2Long(Object obj) {
		return isEmpty(obj) ? 0 : Long.parseLong(obj.toString());
	}

	/**
	 * 转为int类型
	 * 
	 * @param str
	 * @return
	 */
	public static int str2Int(Object obj) {
		return isEmpty(obj) ? 0 : Integer.parseInt(obj.toString());
	}

	/**
	 * 转为float类型
	 * 
	 * @param str
	 * @return
	 */
	public static float str2Float(Object obj) {
		return isEmpty(obj) ? 0 : Float.parseFloat(obj.toString());
	}

	/**
	 * 转为double类型
	 * 
	 * @param str
	 * @return
	 */
	public static double str2Double(Object obj) {
		return isEmpty(obj) ? 0 : Double.parseDouble(obj.toString());
	}

	public static boolean str2Boolean(Object obj) {
		return isEmpty(obj) ? false : Boolean.parseBoolean(obj.toString());
	}

	public static char str2Char(Object obj) {
		return isEmpty(obj) ? ' ' : (Character) obj;
	}

	public static byte str2Byte(Object obj) {
		return isEmpty(obj) ? 0 : Byte.parseByte(obj.toString());
	}

	public static short str2Short(Object obj) {
		return isEmpty(obj) ? 0 : Short.parseShort(obj.toString());
	}

	/**
	 * 数组转为 ('a','b')，空返回('')
	 * 
	 * @param names
	 * @return
	 */
	public static String getStrsplit(String[] names) {
		String rs = "('')";
		if (names != null && !"".equals(names)) {
			StringBuilder result = new StringBuilder("(");
			for (String string : names) {
				result.append("'" + string.toString() + "',");
			}
			rs = result.substring(0, result.length() - 1) + ")";
		}
		return rs;
	}

	/**
	 * 数组链表转为 ('a','b') ，空返回('')
	 * 
	 * @param names
	 * @return
	 */
	public static String getStrsplit(List<?> names) {
		String rs = "('')";
		if (names != null && !names.isEmpty()) {
			StringBuilder result = new StringBuilder("(");
			boolean hasRecord = false;
			for (Object o : names) {
				if (!isEmpty(o)) {
					hasRecord = true;
					result.append("'" + o.toString() + "',");
				}
			}
			if (hasRecord) {
				result.deleteCharAt(result.length() - 1).append(")");
				rs = result.toString();
			}
		}
		return rs;
	}

	/**
	 * 
	 * @return 返回调用者的方法名
	 */
	public static String getCurrentMethodName() {
		return new Exception().getStackTrace()[1].getMethodName();
	}

	public static StringBuffer string2Json(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (c) {
			case '\"':
				sb.append("\\\"");
				break;
			case '\\':
				sb.append("\\\\");
				break;
			case '/':
				sb.append("\\/");
				break;
			case '\b':
				sb.append("\\b");
				break;
			case '\f':
				sb.append("\\f");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\t':
				sb.append("\\t");
				break;
			default:
				sb.append(c);
			}
		}
		return sb;
	}

	public static String getLikeStr(String str) {
		return getLikeStr(str, "");
	}

	public static String getLikeStr(String str, String type) {
		if (type.isEmpty()) {
			str = "%" + str + "%";
		} else if (type.equals("L")) {
			str = "%" + str;
		} else if (type.equals("R")) {
			str = str + "%";
		}
		return str;
	}

	/**
	 * list转map
	 * 
	 * @param list
	 * @return
	 */
	public static Map<String, String> listToMap(List<Object[]> list) {
		Map<String, String> map = new HashMap<String, String>();
		for (Object[] obj : list) {
			map.put(null2String(obj[0]), null2String(obj[1]));
		}
		return map;
	}

	/**
	 * IP转换
	 * 
	 * @param ip
	 * @return
	 */
	public static String convertIp(String ip) {
		if ("59.203.19.236".equals(ip)) {
			return "172.16.55.24";
		} else if ("59.203.19.239".equals(ip)) {
			return "172.16.55.17";
		}
		return ip;
	}

	/**
	 * 生成指定长度随机数
	 * 
	 * @param codeLength
	 * @return
	 */
	public static String getRandCode(int codeLength) {
		char p[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
				'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
		StringBuffer resultCode = new StringBuffer();
		for (int i = 0; i < codeLength; i++) {
			Random rand = new Random();
			int a = rand.nextInt(36);
			resultCode.append(p[a]);
		}
		return resultCode.toString();
	}

	/**
	 * 功能：判断两个可以转换成数组的字符串是否相等
	 * 
	 * @return
	 */
	public static boolean arrayIsEquals(String str1, String str2) {
		str1 = str1.replaceAll(" ", "");
		str2 = str2.replaceAll(" ", "");
		String arr1[] = str1.split(",");
		String arr2[] = str2.split(",");
		Arrays.sort(arr1);
		Arrays.sort(arr2);
		return Arrays.deepEquals(arr1, arr2);
	}
	
	public static boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
	}
	
	/**
	 * 如果taskNos是以“;”分割，转成以“,”分割
	 * 
	 * @param taskNos
	 * @return
	 */
	public static String dealTaskNos(String taskNos) {
		if (taskNos.contains(";")) {
			taskNos = taskNos.replace(";", ",");
		}
		if (taskNos.contains("；")) {
			taskNos = taskNos.replace("；", ",");
		}
		if (taskNos.contains("，")) {
			taskNos = taskNos.replace("，", ",");
		}
		return taskNos;
	}
}
